home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / performance / memory / tinymeter / source / tinymeter_main / initclean.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-20  |  8.7 KB  |  357 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dos.h>
  4. #include <dos/dosextens.h>
  5. #include <intuition/intuition.h>
  6. #include <intuition/gadgetclass.h>
  7. #include <intuition/intuitionbase.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/imageclass.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <intuition/cghooks.h>
  12. #include <intuition/icclass.h>
  13. #include <intuition/classes.h>
  14. #include <intuition/sghooks.h>
  15. #include <intuition/screens.h>
  16. #include <datatypes/datatypesclass.h>
  17. #include <datatypes/datatypes.h>
  18. #include <datatypes/pictureclass.h>
  19. #include <graphics/gfxbase.h>
  20. #include <graphics/text.h>
  21. #include <graphics/gfxmacros.h>
  22. #include <utility/tagitem.h>
  23. #include <utility/hooks.h>
  24. #include <string.h>
  25. #include <clib/macros.h>
  26. #include "gaugeclass.h"
  27. #include "tinymeter.h"
  28.  
  29. extern struct IntuitionBase *IntuitionBase;
  30.  
  31. struct Gadget my_drag=
  32. {
  33.     NULL,
  34.     0,1,
  35.     0,0,
  36.     GFLG_GADGHNONE,
  37.     GACT_IMMEDIATE,
  38.     GTYP_SYSGADGET|GTYP_WDRAGGING,
  39.     0,
  40.     0,
  41.     0,
  42.  
  43.     0,
  44.     0,
  45.     2,
  46.     0
  47. };
  48.  
  49. struct Gadget my_close=
  50. {
  51.     NULL,
  52.     0,0,
  53.     16,16,
  54.     NULL,
  55.     GACT_IMMEDIATE,
  56.     GTYP_SYSGADGET|GTYP_CLOSE,
  57.     0,
  58.     0,
  59.     0,
  60.  
  61.     0,
  62.     0,
  63.     1,
  64.     0
  65. };
  66.  
  67. struct Gadget my_size=
  68. {
  69.     NULL,
  70.     0,0,
  71.     16,16,
  72.     GFLG_GADGHNONE,
  73.     GACT_IMMEDIATE,
  74.     GTYP_SYSGADGET|GTYP_SIZING,
  75.     0,
  76.     0,
  77.     0,
  78.  
  79.     0,
  80.     0,
  81.     3,
  82.     0
  83. };
  84.  
  85. new_window_size(struct tm_sys_set *set,struct tm_data *data)
  86. {
  87.     struct Window   *win=data->win;
  88.     my_drag.Width   =win->Width;
  89.     my_drag.Height  =win->Height-17;
  90.     my_size.TopEdge =win->Height-16;
  91.     my_size.LeftEdge=win->Width -16;
  92. }
  93.  
  94. extern struct  TextAttr topaz_font;
  95.  
  96. struct TextFont *loadFont(char *name, UWORD size)
  97. {
  98.     struct  TextFont     *tf;
  99.     struct  TextAttr      my_text_attr;
  100.  
  101.     my_text_attr.ta_Name =(UBYTE *)name;
  102.     my_text_attr.ta_YSize=(UWORD)size;
  103.     my_text_attr.ta_Flags=FPF_DISKFONT | FPB_PROPORTIONAL;
  104.  
  105.     tf=(struct TextFont *)OpenDiskFont((struct TextAttr *)&my_text_attr);
  106.     if(!tf)
  107.     {
  108.     tf=(struct TextFont *)OpenFont(&topaz_font);
  109.     if(!tf) return(0L);
  110.     }
  111.     return(tf);
  112. }
  113.  
  114. struct Window *open_new_window(struct tm_sys_set *set,struct tm_data *data, UWORD ysiz)
  115. {
  116.     struct Window *window;
  117.  
  118.     if((window=(struct Window *)OpenWindowTags(0,
  119.            WA_Left,            set->x_pos,
  120.            WA_Top,             set->y_pos,
  121.            WA_Width,           set->x_siz,
  122.            WA_MaxWidth,        4096,
  123.            WA_MaxHeight,       ysiz,
  124.            WA_MinHeight,       ysiz,
  125.            WA_Height,          ysiz,
  126.            WA_MinWidth,        data->min_x_size,
  127.            WA_Flags,           (set->win_backfront!=win_back ? 0 : WFLG_BACKDROP )|WFLG_BORDERLESS|WFLG_RMBTRAP,
  128.            WA_PubScreen,       data->scr,
  129.            TAG_DONE)))
  130.     {
  131.     my_drag.Width=   window->Width;
  132.     my_drag.Height=  window->Height-17;
  133.     my_size.TopEdge= window->Height-16;
  134.     my_size.LeftEdge=window->Width -16;
  135.     if(set->win_move==win_normal)
  136.     {
  137.         AddGadget(window,&my_drag, (UWORD)0);
  138.         AddGadget(window,&my_size, (UWORD)0);
  139.         AddGadget(window,&my_close,(UWORD)0);
  140.     }
  141.     ModifyIDCMP(window,IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW|IDCMP_CHANGEWINDOW|IDCMP_SIZEVERIFY|IDCMP_VANILLAKEY|IDCMP_MOUSEBUTTONS);
  142.     return(window);
  143.     }
  144.     return(0L);
  145. }
  146.  
  147. struct Window *snapBackground(struct tm_sys_set *set,struct tm_data *data)
  148. {
  149.     struct Window *win=data->win;
  150.     UWORD x,y,xs,ys;
  151.  
  152.     x=win->LeftEdge;
  153.     y=win->TopEdge;
  154.     xs=win->Width;
  155.     ys=win->Height;
  156.     set->x_pos=x;
  157.     set->y_pos=y;
  158.     set->x_siz=xs;
  159.  
  160.     if(data->bg_bm) FreeBitMap(data->bg_bm);
  161.     if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,data->scr->RastPort.BitMap->Depth,0,0))
  162.     {
  163.     RemoveGList(win,win->FirstGadget,-1);
  164.     CloseWindow(win);
  165.     BltBitMap(data->scr->RastPort.BitMap,x,y,data->bg_bm,0,0,xs,ys,0xc0,0xff,0);
  166.     return((struct Window *)open_new_window(set,data,ys));
  167.     }
  168.     return(0L);
  169. }
  170.  
  171. void CopyTiledBitMap(struct BitMap *Src,WORD SrcSizeX,WORD SrcSizeY,WORD DstSizeX,WORD DstSizeY ,struct BitMap *Dst)
  172. {
  173.     WORD PosX,
  174.      PosY;
  175.     WORD SizeX,
  176.      SizeY;
  177.     for (PosX = 0,SizeX = MIN(SrcSizeX,DstSizeX);PosX<DstSizeX;)
  178.     {
  179.     for (PosY = 0,SizeY = MIN(SrcSizeY,DstSizeY);PosY<DstSizeY;)
  180.     {
  181.         BltBitMap(Src,0,0,Dst,PosX,PosY,SizeX,SizeY,0xC0,0xff,0L);
  182.         PosY += MIN(SizeY,DstSizeY-PosY);
  183.         SizeY = MIN(SizeY,DstSizeY-PosY);
  184.     }
  185.     PosX += MIN(SizeX,DstSizeX-PosX);
  186.     SizeX = MIN(SizeX,DstSizeX-PosX);
  187.     }
  188. }
  189.  
  190. fileBackground(struct tm_sys_set *set,struct tm_data *data)
  191. {
  192.     struct Window       *win=data->win;
  193.     struct BitMap       *work;
  194.     struct BitMapHeader *header;
  195.     UWORD xs,ys;
  196.  
  197.     if (data->bg_bm){ FreeBitMap(data->bg_bm); data->bg_bm=0L; }
  198.     if (data->dt_object = (Object *)NewDTObject(&set->bg_picture[0],
  199.                         DTA_SourceType       ,DTST_FILE,
  200.                         DTA_GroupID          ,GID_PICTURE,
  201.                         PDTA_Remap           ,TRUE,
  202.                         PDTA_Screen          ,data->scr,
  203.                         PDTA_FreeSourceBitMap,TRUE,
  204.                         OBP_Precision        ,PRECISION_IMAGE,
  205.                         TAG_DONE))
  206.     {
  207.     if ( DoDTMethod(data->dt_object,NULL,NULL,DTM_PROCLAYOUT,NULL,1))
  208.     {
  209.         GetDTAttrs(data->dt_object,PDTA_BitMapHeader,&header,PDTA_DestBitMap,&work,TAG_DONE);
  210.         if (work==FALSE) GetDTAttrs(data->dt_object,PDTA_BitMap,&work,TAG_DONE);
  211.  
  212.         xs=win->Width;
  213.         ys=win->Height;
  214.         if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,work->Depth,0,0))
  215.         {
  216.         CopyTiledBitMap(work,header->bmh_Width,header->bmh_Height,xs,ys,data->bg_bm);
  217.         }
  218.     }
  219.     }
  220. }
  221.  
  222. ULONG obtainPen( struct tm_data *data, struct GAU_Color *scr)
  223. {
  224.     return(scr->pen ? scr->red : ObtainBestPenA(data->scr->ViewPort.ColorMap, scr->red, scr->green, scr->blue, 0L) );
  225. }
  226.  
  227. struct Window *openWindow(struct tm_sys_set *set,struct tm_data *data)
  228. {
  229.     UWORD               y_siz,
  230.             line,
  231.             i,
  232.             tmp;
  233.     struct tm_gau_set   *many;
  234.     struct RastPort     *tmpras;
  235.  
  236.     if( data->scr=(struct Screen *)LockPubScreen((char *)&set->pub_name[0]))
  237.     {
  238.     data->on_public=TRUE;
  239.     }
  240.     else
  241.     {
  242.     data->scr=(struct Screen *)IntuitionBase->FirstScreen;
  243.     data->on_public=FALSE;
  244.     }
  245.  
  246.     /* Calc Height of lines, labelpos (starting point of gauges) and open fonts for that... */
  247.  
  248.     data->labelpos=0;
  249.     if(tmpras=(struct RastPort *)AllocVec(sizeof(struct RastPort),MEMF_CLEAR))
  250.     {
  251.     InitRastPort(tmpras);
  252.     for(i=0;i<data->num_of_gaug;i++)
  253.     {
  254.         data->gauge_y_size[i]=0;
  255.         data->gauge_y_size_falling[i]=0;
  256.     }
  257.     for(i=0,many=data->list;i<data->num_of_gaug;i++)
  258.     {
  259.         if(many->type!=typ_none)
  260.         {
  261.         line=(i/(set->colums));
  262.         if(data->Font[i]=loadFont(many->font,many->font_size))
  263.             data->gauge_y_size[line]=MAX(data->gauge_y_size[line],data->gauge_y_size_falling[i]=(data->Font[i]->tf_YSize*(many->size_y+100))/100);
  264.         if((many->gauge_type!=typ_histmeter)&&(many->type!=typ_clock_))
  265.         {
  266.             SetFont(tmpras,data->Font[i]);
  267.             tmp=TextLength(tmpras,many->label,my_strlen(many->label));
  268.             if(tmp>data->labelpos)data->labelpos=tmp;
  269.         }
  270.         }
  271.         else data->Font[i]=0;
  272.         many=many->next;
  273.     }
  274.     data->labelpos+=4; FreeVec(tmpras);
  275.  
  276.     /* Setup used lines and gauge_x_size */
  277.     data->num_of_rows   =((data->num_of_gaug-1)/set->colums)+1;
  278.     data->min_x_size    =((data->labelpos+8)*set->colums)+(set->win_space_x*(set->colums-1))+(set->win_border_x<<1);
  279.     if(data->min_x_size>set->x_siz)set->x_siz=data->min_x_size;
  280.     data->gauge_x_size  =(set->x_siz-(set->win_border_x<<1)-((set->colums-1)*set->win_space_x))/set->colums;
  281.  
  282.     /* Setup Window Height */
  283.     if(set->lay_falling)
  284.     {
  285.         UWORD old_siz,j;
  286.         for(i=0,y_siz=0;i<set->colums;i++)
  287.         {
  288.         old_siz=y_siz;
  289.         for(j=i,y_siz=0;j<data->num_of_gaug;j+=set->colums) y_siz+=data->gauge_y_size_falling[j];
  290.         if(y_siz>old_siz) old_siz=y_siz;
  291.         }
  292.         y_siz=old_siz;
  293.     }
  294.     else for(i=0,y_siz=0;i<data->num_of_rows;i++)y_siz+=data->gauge_y_size[i];
  295.     y_siz+=(set->win_border_y<<1)+(set->win_space_y*(data->num_of_rows-1));
  296.  
  297.  
  298.     if(data->win=(struct Window *)open_new_window(set,data,y_siz))
  299.     {
  300.         data->bg_color      =obtainPen(data,&set->bg_color);
  301.         data->bright_color  =obtainPen(data,&set->bright_color);
  302.         data->dark_color    =obtainPen(data,&set->dark_color);
  303.         switch (set->bg_type)
  304.         {
  305.         case    bg_file:
  306.             fileBackground(set,data);
  307.             return(data->win);
  308.             break;
  309.         case    bg_snap:
  310.             return(data->win=snapBackground(set,data));
  311.             break;
  312.         default:
  313.             return(data->win);
  314.             break;
  315.         }
  316.     }
  317.     }
  318.     return(0L);
  319. }
  320.  
  321. closeWindow(struct tm_sys_set *set,struct tm_data *data)
  322. {
  323.     int i;
  324.  
  325.     if(data->win)
  326.     {
  327.     RemoveGList(data->win,data->win->FirstGadget,-1);
  328.     CloseWindow(data->win);
  329.     data->win=0L;
  330.     }
  331.  
  332.     for(i=0;i<data->num_of_gaug;i++)
  333.     if(data->Font[i])
  334.     {
  335.         CloseFont(data->Font[i]);
  336.         data->Font[i]=0L;
  337.     }
  338.  
  339.     if(data->on_public)
  340.     {
  341.     UnlockPubScreen((char *)&set->pub_name[0]);
  342.     data->on_public=FALSE;
  343.     }
  344.  
  345.     if(data->dt_object)
  346.     {
  347.     DisposeDTObject((Object *)data->dt_object);
  348.     data->dt_object=0L;
  349.     }
  350.  
  351.     if(data->bg_bm)
  352.     {
  353.     FreeBitMap(data->bg_bm);
  354.     data->bg_bm=0L;
  355.     }
  356. }
  357.